package com.hzdracom.epubreader.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.inject.Inject;
import com.hzdracom.epubreader.activity.ReadingEpubActivity;
import com.shwread.android.qysw10000243.R;

import net.nightwhistler.pageturner.activity.ReadingFragment;
import net.nightwhistler.pageturner.bookmark.BookmarkDatabaseHelper;
import net.nightwhistler.pageturner.view.HighlightManager;
import net.nightwhistler.pageturner.view.NavigationCallback;
import net.nightwhistler.pageturner.view.bookview.TextLoader;

import java.util.ArrayList;
import java.util.List;

import roboguice.fragment.RoboFragment;
import roboguice.inject.InjectView;

/**
 * 目录书签
 */
public class ContentBookmarkFragment extends RoboFragment implements OnClickListener {

	@InjectView(R.id.btnContents)
	private Button btnContents;

	@InjectView(R.id.btnBookmarks)
	private Button btnBookmarks;

	@InjectView(R.id.lstContents)
	private ListView lstContents;

	@InjectView(R.id.lstBookmarks)
	private ListView lstBookmarks;

	@InjectView(R.id.layoutRightBarInFragment)
	private RelativeLayout layoutLeftBar;

	@InjectView(R.id.txtNoBookmarkTip)
	private TextView txtNoBookmarkTip;

	@Inject
	private TextLoader textLoader;

	@Inject
	private HighlightManager highlightManager;

	@Inject
	private BookmarkDatabaseHelper bookmarkDatabaseHelper;

	private boolean isShowContents = true;

	private BookContentListAdapter contentAdapter;
	private BookmarkListAdapter bookmarkAdapter;

	private ReadingEpubActivity readingActivity;

	private int colorSelected;
	private int colorNoneSelected;

	private ReadingFragment readingFragment;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.epub3_fragment_content_bookmark, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		colorSelected = Color.rgb(0, 104, 183);
		colorNoneSelected = Color.rgb(128, 128, 128);

		// 设置按钮事件和其他事项
		btnBookmarks.setOnClickListener(this);
		btnContents.setOnClickListener(this);
		contentAdapter = new BookContentListAdapter(getActivity());
		bookmarkAdapter = new BookmarkListAdapter(getActivity());
		this.lstContents.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				contentAdapter.getContentsCallbacks().get(position).onClick();
				if (readingActivity != null) {
					readingActivity.closeNavigationDrawer();
				}
				readingFragment.hideToolbar();
			}
		});
		this.lstContents.setAdapter(contentAdapter);

		this.lstBookmarks.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				bookmarkAdapter.getContentsCallbacks().get(position).onClick();
				if (readingActivity != null) {
					readingActivity.closeNavigationDrawer();
				}
				readingFragment.hideToolbar();
			}
		});
		this.lstBookmarks.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				bookmarkAdapter.getContentsCallbacks().get(position).onLongClick();
				return true;
			}
		});
		this.lstBookmarks.setAdapter(bookmarkAdapter);

		layoutLeftBar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ReadingEpubActivity.instance.closeNavigationDrawer();
			}
		});
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.btnBookmarks) {
			if (isShowContents) {
				isShowContents = !isShowContents;
				this.lstBookmarks.setVisibility(View.VISIBLE);
				this.lstContents.setVisibility(View.GONE);
				this.btnBookmarks.setTextColor(colorSelected);
				this.btnContents.setTextColor(colorNoneSelected);

				if (bookmarkAdapter.getContentsCallbacks() == null
						|| bookmarkAdapter.getContentsCallbacks().size() == 0) {
					this.txtNoBookmarkTip.setVisibility(View.VISIBLE);
				} else {
					this.txtNoBookmarkTip.setVisibility(View.GONE);
				}
			}
		} else if (id == R.id.btnContents) {
			if (!isShowContents) {
				isShowContents = !isShowContents;
				this.lstBookmarks.setVisibility(View.GONE);
				this.lstContents.setVisibility(View.VISIBLE);
				this.btnContents.setTextColor(colorSelected);
				this.btnBookmarks.setTextColor(colorNoneSelected);
			}
		}
	}

	/**
	 * 初始化目录和书签信息
	 */
	public void initContentsBookmarks(ReadingFragment readingFragment, ReadingEpubActivity readingActivity) {
		this.readingActivity = readingActivity;
		this.readingFragment = readingFragment;

		this.contentAdapter.setContentsCallbacks(readingFragment.getTableOfContents());
		if (readingFragment.getBookView().getSpine() != null) {
			this.contentAdapter.setCurrChapterHref(readingFragment.getBookView().getSpine().getCurrentHref());
		}
		this.contentAdapter.notifyDataSetChanged();
		updateContentListPosition();

		// 书签信息
		this.bookmarkAdapter.setContentsCallbacks(readingFragment.getBookmarks());
		this.bookmarkAdapter.notifyDataSetChanged();
		lstContents.setSelection(contentAdapter.getCurrPosition());
		
		if (bookmarkAdapter.getContentsCallbacks() == null
				|| bookmarkAdapter.getContentsCallbacks().size() == 0) {
			this.txtNoBookmarkTip.setVisibility(View.VISIBLE);
		} else {
			this.txtNoBookmarkTip.setVisibility(View.GONE);
		}
	}

	public void updateContentListPosition() {
		if (android.os.Build.VERSION.SDK_INT >= 8) {
			lstContents.smoothScrollToPosition(contentAdapter.getCurrPosition());
		} else {
			lstContents.setSelection(contentAdapter.getCurrPosition());
		}
	}

	/**
	 * 目录Adapter
	 * 
	 * @author dave
	 * 
	 */
	class BookContentListAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		List<NavigationCallback> contentsCallbacks;
		int currPosition = -1;

		public int getCurrPosition() {
			return currPosition;
		}

		public List<NavigationCallback> getContentsCallbacks() {
			return contentsCallbacks;
		}

		private String currChapterHref;

		public void setCurrChapterHref(String currChapterHref) {
			this.currChapterHref = currChapterHref;
		}

		public void setContentsCallbacks(List<NavigationCallback> contentsCallbacks) {
			this.contentsCallbacks = contentsCallbacks;
		}

		public BookContentListAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			contentsCallbacks = new ArrayList<NavigationCallback>(1);
		}

		@Override
		public int getCount() {
			return contentsCallbacks.size();
		}

		@Override
		public Object getItem(int position) {
			return contentsCallbacks.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolderTableContent holder;

			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.epub3_table_content_row, null);
				holder = new ViewHolderTableContent();
				holder.txtContent = (TextView) convertView.findViewById(R.id.txtTableCotentItem);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderTableContent) convertView.getTag();
			}

			NavigationCallback info = contentsCallbacks.get(position);
			holder.txtContent.setText(info.getTitle());
			if (currChapterHref.equals(info.getSubtitle())) {
				currPosition = position;
				holder.txtContent.setTextColor(Color.rgb(0, 104, 183));
				ContentBookmarkFragment.this.updateContentListPosition();
			} else {
				holder.txtContent.setTextColor(Color.rgb(128, 128, 128));
			}
			return convertView;
		}

	}

	/**
	 * 书签Adapter
	 * 
	 * @author dave
	 * 
	 */
	class BookmarkListAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		List<NavigationCallback> contentsCallbacks;

		public List<NavigationCallback> getContentsCallbacks() {
			return contentsCallbacks;
		}

		public void setContentsCallbacks(List<NavigationCallback> contentsCallbacks) {
			this.contentsCallbacks = contentsCallbacks;
		}

		public BookmarkListAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			contentsCallbacks = new ArrayList<NavigationCallback>(1);
		}

		@Override
		public int getCount() {
			return contentsCallbacks.size();
		}

		@Override
		public Object getItem(int position) {
			return contentsCallbacks.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolderBookmark holder;

			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.epub3_bookmark_row, null);
				holder = new ViewHolderBookmark();
				holder.txtTitle = (TextView) convertView.findViewById(R.id.txtBookmarkTitle);
				holder.txtSubTitle = (TextView) convertView.findViewById(R.id.txtBookmarkSubTitle);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderBookmark) convertView.getTag();
			}

			NavigationCallback info = contentsCallbacks.get(position);
			holder.txtTitle.setText(info.getTitle());
			holder.txtSubTitle.setText(info.getSubtitle());
			return convertView;
		}

	}

	static class ViewHolderBookmark {
		TextView txtTitle;
		TextView txtSubTitle;
	}

	static class ViewHolderTableContent {
		TextView txtContent;
	}
}
